package com.excel.controller;

import com.alibaba.fastjson.JSONObject;
import com.excel.common.ExcelUtils;
import exam.common.Result;
import exam.file.utils.FileOutUtils;
import exam.note.mapper.NoteMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/18 1:48
 * @since JDK 1.8
 */
@RestController
@Api("excel文件导出")
public class ExcelController {

    @Autowired
    private NoteMapper noteMapper;

    @GetMapping("excelexport")
    @ApiOperation("测试文件导出")
    public JSONObject exportLogController(HttpServletResponse response){
        List<Map<String, Object>> object=noteMapper.selectMaps(null);
        Map<String, String> stringStringMap=new HashMap<>();
        stringStringMap.put("title","标题");
        stringStringMap.put("note","备注");
        stringStringMap.put("typeid","类型id");
        stringStringMap.put("id","主键id");
        String path= ExcelUtils.commonExcelMapList("测试文件",object,stringStringMap);
        FileOutUtils.fileOutByPath(path,response);
        File file=new File(path);
        if(file.exists()){
            file.delete();
        }
        return Result.successObject();
    }
}
